/* JSDAS
* Copyright (C) 2008-2009 Bernat Gel
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @author Jose Villaveces 
 */

 YUI({combine: true, timeout: 10000}).use("console", "test",function (Y) {
			 
			 //=========================== Test Case for URL Managing =============================
						
				var URLManaging = new Y.Test.Case({ 
					name: "URL Managing",
							         
					testGetSourceURL : function () {
							Y.log("Testing getSourceURL", "info",  "Log");                            
							Y.Assert.areEqual(JSDAS.URL.getSourceURL("http://das.sanger.ac.uk/das/cosmic_transcripts/features?segment=7:139887581,140077687", 'test_source'),'http://das.sanger.ac.uk/das/test_source/','Assert 1');
							Y.Assert.areEqual(JSDAS.URL.getSourceURL("http://das.sanger.ac.uk/das/cosmic_transcripts/features?segment=7:139887581,140077687", 'test_source/'),'http://das.sanger.ac.uk/das/test_source/','Assert 2');
							Y.Assert.areEqual(JSDAS.URL.getSourceURL("http://das.sanger.ac.uk/das/cosmic_transcripts/features?segment=7:139887581,140077687"),'http://das.sanger.ac.uk/das/cosmic_transcripts/','Assert 3');
							Y.Assert.areEqual(JSDAS.URL.getSourceURL("http://das.sanger.ac.uk/some/thing/else/das/cosmic_transcripts/features?segment=7:139887581,140077687", 'test_source/'),'http://das.sanger.ac.uk/some/thing/else/das/test_source/','Assert 4');
							Y.Assert.areEqual(JSDAS.URL.getSourceURL("http://das.sanger.ac.uk/some/thing/else/das/cosmic_transcripts/features?segment=7:139887581,140077687"),'http://das.sanger.ac.uk/some/thing/else/das/cosmic_transcripts/','Assert 5');
							Y.Assert.areEqual(JSDAS.URL.getSourceURL("http://das.sanger.ac.uk/das/"),'error', 'Assert 6');
					}, 
					
					testCommandsURL : function (){
						Y.log("Testing getEntryPointsURL", "info",  "Log");
						Y.Assert.areEqual(JSDAS.URL.getEntryPointsURL("http://das.sanger.ac.uk/das/cosmic_transcripts/features?segment=7:139887581,140077687", 'test_source'),'http://das.sanger.ac.uk/das/test_source/entry_points');
						Y.log("Testing getTypesURL", "info",  "Log");
						Y.Assert.areEqual(JSDAS.URL.getTypesURL("http://das.sanger.ac.uk/das/cosmic_transcripts/features?segment=7:139887581,140077687", 'test_source'),'http://das.sanger.ac.uk/das/test_source/types');
						Y.log("Testing getFeaturesURL", "info",  "Log");
						Y.Assert.areEqual(JSDAS.URL.getFeaturesURL("http://das.sanger.ac.uk/some/thing/else/das/cosmic_transcripts/features?segment=7:139887581,140077687"),'http://das.sanger.ac.uk/some/thing/else/das/cosmic_transcripts/features');
					},
					
					testAnalizeURL : function (){
						
						Y.log("Testing analyzeURL", "info",  "Log");
						
						var comObj1 = {
									prefix: 'http://das.sanger.ac.uk/',
									source: 'cosmic_transcripts',
									command: 'features',
									parameters: 'segment=7:139887581,140077687'
								};
						
						var obj1 = JSDAS.URL.analyzeUrl("http://das.sanger.ac.uk/das/cosmic_transcripts/features?segment=7:139887581,140077687")
						
						for(key in obj1) {
							Y.assert(obj1[key]==comObj1[key], 'Assert 1');
						}
						
						var comObj2 = {
									prefix: 'http://das.sanger.ac.uk/',
									source: undefined,
									command: undefined,
									parameters: undefined
								};
						
						var obj2 = JSDAS.URL.analyzeUrl("http://das.sanger.ac.uk/das/");
						
						for(key in obj2) {
							Y.assert(obj2[key]==comObj2[key], 'Assert 2');
						}
						
						var obj3 = {
									prefix: 'http://das.sanger.ac.uk/',
									source: 'a_source',
									command: 'features',
									parameters: 'fake_arg=http://a.test.com/das/links'
								};
						
						var comObj3 = JSDAS.URL.analyzeUrl("http://das.sanger.ac.uk/das/a_source/features?fake_arg=http://a.test.com/das/links");
						
						for(key in obj3) {
							Y.assert(obj3[key]==comObj3[key], 'Assert 2');
						}
					}
				});
				
			 //=========================== Test Case for XMLLoader =============================
			 	var fake_url;
				var valid_url_not_xml;
				var xml_not_das;
				
				var XMLLoader = new Y.Test.Case({
					name: "XMLLoader",
					
					setUp: function (){
						fake_url = 'http://testurl.notreal';
						valid_url_not_xml = 'http://www.google.com';
						xml_not_das = "http://www.bbc.co.uk/home/features/d/snippet.xml";
						Y.log("Testing Callbacks", "info",  "Log");
					},
					tearDown:function(){
						fake_url = undefined;
						xml_not_das = undefined;
						valid_url_not_xml = undefined;
					},
					
					testFakeURLCall : function (){
						
						Y.log("Testing Fake URL Call", "info",  "Log");
						
						var e=undefined;
						JSDAS.XMLLoader.load(fake_url, function(el) {e=el;}, function(el) {e=el;});
						
						this.wait(function(){
							Y.Assert.isNotUndefined(e);							
							Y.Assert.areEqual(e.id, 'xmlhttprequest_error');
							
						},1000);
									
					},
					
					testValidURLNoXML : function (){
						Y.log("Testing Valid URL Returning No XML", "info",  "Log");
						
						var e=undefined;
						JSDAS.XMLLoader.load(valid_url_not_xml, function(el) {e=el;}, function(el) {e=el;});
							
						this.wait(function(){
							Y.Assert.isNotUndefined(e);							
							Y.Assert.areEqual(e.id, 'not_xml');
						},1000);
					},
					
					/*
					 * TO DO: We have not yet decided how to handle these exceptions.
					 */
					testValidURLNoDASXML : function (){
						/*Y.log("Testing Valid URL Returning No DAS XML", "info",  "Log");
						
						var e=undefined;
						JSDAS.XMLLoader.load(xml_not_das, function(el) {e=el;}, function(el) {e=el;});
							
						this.wait(function(){
							Y.Assert.isNotUndefined(e);
							alert(e.id);							
							Y.Assert.areEqual(e.id, 'not_xml');
						},1000);*/
					}
				});
			 
			 	 //=========================== Test Case for Basic API =============================
			 	var ensembl_human;
				
				var BasicTest = new Y.Test.Case({
					name: "Basic API Test",
					
					setUp: function (){
						ensembl_human = "http://www.ensembl.org/das/Homo_sapiens.NCBI36.reference/";
					},
					tearDown:function(){
						ensembl_human = undefined;
					},
					
					testSequence : function (){
						
						Y.log("Testing Sequence", "info",  "Log");
						var sequence_url = JSDAS.URL.getSequenceURL(ensembl_human, 'Homo_sapiens.NCBI36.reference');
						
						Y.assert(sequence_url == "http://www.ensembl.org/das/Homo_sapiens.NCBI36.reference/sequence");
						
						var e=undefined;
						JSDAS.sequence(sequence_url+"?segment=1:10000000,10000010", function(el) {e=el;});
						this.wait(function(){
							Y.Assert.isNotUndefined(e);							
							Y.Assert.areEqual(e.SEQUENCE[0].textContent, 'aaccccgtctc');
						},1000);
									
					}			
				});
				
				//=========================== Test Case for Error Handling =============================
				
				var ErrorTest = new Y.Test.Case({
					name: "Error Test",
					
					setUp: function (){
						JSDAS.flushErrors();
					},
					tearDown:function(){
						JSDAS.flushErrors();
					},
					
					testErrors : function (){
						Y.log("Testing the error handling methods", "info",  "Log");
						Y.assert(JSDAS.error == false);
						
						var i = 0;
						while( i < 3){
							JSDAS.addError("id"+i, "comment"+i);
							i++;
						}
						
						Y.assert(JSDAS.error == true);
						Y.assert(JSDAS.errors.length == 3);
						
						errors = JSDAS.errors;
						i = 0;
						while( i < errors.length){
							var obj = errors[i];
							Y.assert(errors[i].id == "id"+i);
							Y.assert(errors[i].comment == "comment"+i);
							i++;
						}
						
						JSDAS.flushErrors();
						Y.assert(JSDAS.error == false);
					}
				});
				
			    //Create the console
			    var c = new Y.Console({
			        newestOnTop : false,
			        style: 'block'
			    });
			    
			    c.render('#testLogger');
			    
			    Y.Test.Runner.add(URLManaging);
				Y.Test.Runner.add(XMLLoader);
				Y.Test.Runner.add(BasicTest);
				Y.Test.Runner.add(ErrorTest);
				
			    //Run the tests
			    Y.Test.Runner.run();
			 
});